package com.newtouch.bxzs.business.statistics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.personal.mapper.PersonalMapper;
import com.newtouch.bxzs.business.statistics.mapper.MemberReceiveSumMapper;
import com.newtouch.bxzs.business.statistics.mapper.OrganizationReceiveSumMapper;
import com.newtouch.bxzs.business.statistics.model.bo.MemberReceiveSumBO;
import com.newtouch.bxzs.business.statistics.model.bo.OrganizationReceiveSumBO;
import com.newtouch.bxzs.business.statistics.model.vo.MemberReceiveSumVO;
import com.newtouch.bxzs.business.statistics.model.vo.OrganizationReceiveSumVO;
import com.newtouch.bxzs.business.statistics.service.MemberReceiveSumService;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.em.SystemParamsEnum;
import com.newtouch.bxzs.common.base.model.User;
import com.newtouch.bxzs.common.base.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 机构意向接收汇总查询
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/27 10:17</td>
 * <td>lingshouyi</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author lingshouyi
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class MemberReceiveSumServiceImpl implements MemberReceiveSumService {

    @Autowired
    private MemberReceiveSumMapper memberReceiveSumMapper;

    @Autowired
    private OrganizationReceiveSumMapper organizationReceiveSumMapper;

    @Autowired
    private PersonalMapper personalMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Value("${maxTime}")
    private String maxTime;

    @Override
    public IPage<MemberReceiveSumBO> getMemberReceiveSumList(MemberReceiveSumVO memberReceiveSumVO) {
        if (BeanUtil.isEmpty(memberReceiveSumVO)) {
            return null;
        }
        //修改截止日期
        memberReceiveSumVO.setEndTime(DateUtil.offsetDay(memberReceiveSumVO.getEndTime(), 1));
        /*根据机构编码获取该机构业务员*/
        List<String> userIdList = new ArrayList<>();
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("USER_ORGANIZATION_CODE", memberReceiveSumVO.getOrganizationCode());
        /*获取id*/
        if(StringUtils.isEmpty(memberReceiveSumVO.getReceiveUserId())){
            for (User user : personalMapper.selectList(qw)) {
                userIdList.add(user.getUserId());
            }
        }else {
            userIdList.add(memberReceiveSumVO.getReceiveUserId());
        }
        /*分页*/
        Page<MemberReceiveSumBO> page = new Page<>(memberReceiveSumVO.getCurrentPage(), memberReceiveSumVO.getPageSize());
        //获取提醒升级时间
        String limit = null;
        try {
            limit = redisUtils.get(SystemParamsEnum.REMIND_TO_UPGRADE.getCode());
        }catch (Exception e){
            throw new BaseException("-1","未配置提醒升级期限");
        }
        IPage<MemberReceiveSumBO> memberReceiveSumList = memberReceiveSumMapper.getMemberReceiveSum(page, userIdList, DateUtil.beginOfDay(memberReceiveSumVO.getStartTime()),DateUtil.endOfDay(memberReceiveSumVO.getEndTime()), limit);
        /*排名*/
        Integer n = 1;
        for (MemberReceiveSumBO memberReceiveSumBO : memberReceiveSumList.getRecords()) {
            memberReceiveSumBO.setRank(n);
            n++;
        }
        return memberReceiveSumList;
    }

    @Override
    public IPage<OrganizationReceiveSumBO> getOrganizationReceiveSumList(OrganizationReceiveSumVO organizationReceiveSumVO) {
        if (BeanUtil.isEmpty(organizationReceiveSumVO)) {
            return null;
        }
        //修改截止日期
        organizationReceiveSumVO.setEndTime(DateUtil.offsetDay(organizationReceiveSumVO.getEndTime(), 1));
        Page<OrganizationReceiveSumBO> page = new Page<>(organizationReceiveSumVO.getCurrentPage(), organizationReceiveSumVO.getPageSize());
        /*判断是否传入推送人*/
        if (!StringUtils.isEmpty(organizationReceiveSumVO.getReceiveUserId())) {
            /*获取当前推送人的机构编码,将该代码设置到OrganizationCodeList中*/
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("USER_ID", organizationReceiveSumVO.getReceiveUserId());
            /*将推送人的机构编码设置为当前参数*/
            List<String> organizationCodeList = new ArrayList<>();
            organizationCodeList.add(personalMapper.selectList(qw).get(0).getUserOrganizationCode());
            organizationReceiveSumVO.setOrganizationCodeList(organizationCodeList);
        }
        //获取提醒升级时间
        String limit = null;
        try {
            limit = redisUtils.get(SystemParamsEnum.REMIND_TO_UPGRADE.getCode());
        }catch (Exception e){
            throw new BaseException("-1","未配置提醒升级期限");
        }
        return organizationReceiveSumMapper.getOrganizationReceiveSumList(page, organizationReceiveSumVO.getOrganizationCodeList(), DateUtil.beginOfDay(organizationReceiveSumVO.getStartTime()),DateUtil.endOfDay(organizationReceiveSumVO.getEndTime()), limit);
    }
}
